package com.yuanqi.controller;

import com.yuanqi.pojo.Sku;
import com.yuanqi.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试控制器 - 用于验证数据库连接和基本操作
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private SkuService skuService;

    /**
     * 测试数据库连接
     */
    @GetMapping("/db")
    public String testDatabase() {
        try {
            // 尝试查询SKU表
            List<Sku> skus = skuService.list();
            return "数据库连接成功！SKU表记录数: " + skus.size();
        } catch (Exception e) {
            return "数据库连接失败: " + e.getMessage();
        }
    }

    /**
     * 测试创建SKU
     */
    @GetMapping("/create-sku")
    public String testCreateSku() {
        try {
            Sku sku = new Sku();
            sku.setProductId(999L);
            sku.setSkuCode("TEST_" + System.currentTimeMillis());
            sku.setUnitId(1L);
            sku.setStatus(1);
            
            boolean saved = skuService.save(sku);
            if (saved && sku.getId() != null) {
                return "SKU创建成功！ID: " + sku.getId();
            } else {
                return "SKU创建失败";
            }
        } catch (Exception e) {
            return "SKU创建异常: " + e.getMessage();
        }
    }
}

